<TITLE>BGI Documentation for 
mousex
</TITLE>
<H2>
mousex
</H2>
<HR>
<DL>


<DT><B>Syntax</B><DD>
<PRE>
#include "graphics.h"
int mousex(void);
</PRE>



<DT><B>Description</B><IMG SRC="win.gif" ALT="[WIN]"><DD>
The 
mousex
function is available in the
<A HREF="../../bgi/README.html">winbgim</A> implementation of BGI
graphics.
It returns the most recent x coordinate of the mouse within the
graphics window. X-coordinates start with 0 at the left edge of the
window and increase to getmaxx(&nbsp;) at the right edge of the window.

<P><DT><B>Return Value</B><DD>
Most recent x coordinate of the mouse within the graphics window


<P><DT><B>See also</B>
<DD><A HREF="getmaxx.html"> getmaxx </A>
<DD><A HREF="mousey.html"> mousey </A>
<DD><A HREF="registermousehandler.html"> registermousehandler </A>



<P><DT><B>Example</B><DD>
<PRE>
/* mouse example */ 
#include "graphics.h"

// The click_handler will be called whenever the left mouse button is
// clicked. It checks copies the x,y coordinates of the click to
// see if the click was on a red pixel. If so, then the boolean
// variable red_clicked is set to true. Note that in general
// all handlers should be quick. If they need to do more than a little
// work, they should set a variable that will trigger the work going,
// and then return.
bool red_clicked = false;
void click_handler(int x, int y)
{
    if (getpixel(x,y) == RED)
        red_clicked = true;
}

// Call this function to draw an isosoles triangle with the given base and
// height. The triangle will be drawn just above the botton of the screen.
void triangle(int base, int height)
{
    int maxx = getmaxx( );
    int maxy = getmaxy( );

    line(maxx/2 - base/2, maxy - 10, maxx/2 + base/2, maxy - 10);
    line(maxx/2 - base/2, maxy - 10, maxx/2, maxy - 10 - height);
    line(maxx/2 + base/2, maxy - 10, maxx/2, maxy - 10 - height); 
}

void main(void)
{
    int maxx, maxy;  // Maximum x and y pixel coordinates
    int divisor;     // Divisor for the length of a triangle side

    // Put the machine into graphics mode and get the maximum coordinates:
    initwindow(450, 300);         
    maxx = getmaxx( );
    maxy = getmaxy( );

    // Register the function that handles a left mouse click
    registermousehandler(WM_LBUTTONDOWN, click_handler);
    
    // Draw a white circle with red inside and a radius of 50 pixels:
    setfillstyle(SOLID_FILL, RED);
    setcolor(WHITE);
    fillellipse(maxx/2, maxy/2, 50, 50);
    
    // Print a message and wait for a red pixel to be double clicked:
    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
    outtextxy(20, 20, "Left click in RED to end.");
    setcolor(BLUE);
    red_clicked = false;
    divisor = 2;
    while (!red_clicked)
    {
        triangle(maxx/divisor, maxy/divisor);
        delay(500);
        divisor++;
    }

    cout &lt;&lt; "The mouse was clicked at: ";
    cout &lt;&lt; "x=" &lt;&lt; mousex( );
    cout &lt;&lt; " y=" &lt;&lt; mousey( ) &lt;&lt; endl;
    
    // Switch back to text mode:
    closegraph( );
}
</PRE>

</DL>
<HR>
<A HREF="index.html">Back to index</A>
